import { legacy_createStore as createStore, applyMiddleware, compose } from "redux"
import thunkMiddleware from "redux-thunk"
import rootReducer from "./reducers"

const composeEnhancers =
  typeof window === "object" && window["__REDUX_DEVTOOLS_EXTENSION_COMPOSE__"]
    ? window["__REDUX_DEVTOOLS_EXTENSION_COMPOSE__"]({
        // Specify extension’s options like name, actionsBlacklist, actionsCreators, serialize...
      })
    : compose

const middlewares = [thunkMiddleware]

if (process.env.NODE_ENV === "development" && process.env.TARO_ENV !== "quickapp") {
  middlewares.push(require("redux-logger").createLogger())
}

const enhancer = composeEnhancers(
  applyMiddleware(...middlewares)
  // other store enhancers if any
)

/** use root store when you want to get store for process sth out of functional component */
export const withStore = () => {
  return store
}
const store = createStore(rootReducer, enhancer)
/** redux app dispatch */
export type IAppDispatch = typeof store.dispatch
/** redux root store */
export type IRootState = ReturnType<typeof store.getState>
